<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Slider extends CI_Controller {
	var $perpage = 20;

		public function __construct() {
		   parent::__construct();
		   if (!$this->ion_auth->logged_in() || !$this->ion_auth->is_admin()) redirect('auth/login/'.str_replace("/", ".",$_SERVER['REQUEST_URI']));
		   $this->layout->setlayout('admin/layout');
		   $this->crumps->push('/admin/slider/', 'Голосования');
		   $this->users_rights_model->check_rights(strtolower(get_class()), $this->router->get_method());
	   }

	public function index($id = 0)	{
		$data['pages'] = $pages = $this->db->select()->from('slider')->order_by('slide_id', 'desc')->get()->result_array();
		$form = new jQuickForm('simple');
		$random = md5(time()).md5(md5(time()));


		$form->insertInputText('slide_date')->setLabel('Дата окончания акции')->addClass('datepicker')->addRuleRequired();
		
		$select = $form->insertSelect('slide_item')->setLabel('Товар для акции');
		$list = $this->catalog_service_model->get_catalog_from(0, 3);
		foreach ($list as $item) {
			$optgr = $select->addOptGroup($item['parent_title']);
			foreach ($item['childs'] as $gr) {
				$optgr2 = $optgr->addOptGroup("---".$gr['parent_title']);
				foreach ($gr['items'] as $it) {
					$optgr2->addOption("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$it['item_title'], $it['item_id']);
				}
			}
			foreach ($item['items'] as $it2) {
				$optgr->addOption("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".$it2['item_title'], $it2['item_id']);
			}
		}
		
		$form->insertFCKeditor('slide_text')->setLabel('Текст акции');
/*		$post_params = array(
			'random' => $random,
			'max_width' => 990,
			'max_height' => 515,
			'session' => $this->session->userdata('session_id')
		);
		$form->insertMultiUpload('slide_image', '/admin/tools/upload/news/', $post_params, 1, false, 2048000)->setId('slide_image_0')->setLabel('Фотография для голосования')->setComment('Максимум только одна фотография (jpg/png/gif, 1024Kb, 990*515px)');*/

		if ($_SERVER['REQUEST_METHOD'] == "POST" && $form->validate()) {
			$p_data = $this->input->post();
			$p_data['slide_date'] = strtotime($p_data['slide_date']);
			$this->db->insert('slider', $p_data);
			$o = $this->db->insert_id();
			$this->session->set_flashdata('msg', 'Успешно добавлено');
			redirect('admin/slider');
		}

		$form->insertInputSubmit("Добавить");

		$data['form'] = $form."";
		$data['js'] = jQuickForm::js()->getJsAll();

		$this->layout->view('admin/slider/index', $data);
	}
	public function delete($id = 0) {
		if ($id != 0) $data['page'] = $page = $this->db->select()->from('slider')->where('slide_id', $id)->get()->result_array();
		if ($id != 0 && sizeof($page) <= 0) redirect('admin/slider');
		$this->db->where('slide_id', $id)->delete('slider');
		rrmdir('./uploads/main_slider/'.$id);
		$this->session->set_flashdata('msg', 'Успешно удалено');
		redirect('admin/slider');
	}
	public function order($id) {
		$p_data = $this->input->post();
		foreach ($p_data['order'] as $key => $val) {
			$this->db->update('slider', array('slide_order' => $val), array('slide_id' => $key));
		}
		$this->catalog_service_model->updateAllPath();
		$this->session->set_flashdata('msg', 'Успешно');
		redirect('admin/slider/');
	}

}